package com.bf.xpdd.web;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.springframework.stereotype.Controller;

import com.bf.xpdd.domain.ActivityBean;
import com.bf.xpdd.persistent.DaoConfig;
import com.bf.xpdd.persistent.sqlmapdao.DaoActivityImpl;
import com.bf.xpdd.util.Constant;

/**
 * 自启动服务,用于在tomcat启动时启动定时线程处理活动的开启\结束
 * 
 * @author linmu
 * 
 */
//@Component("activityStatusServlet")
@Controller
public class ActivityStatusServlet extends HttpServlet
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -450764733828504422L;

	/**
	 * 线程定时调度池
	 */
	private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

	/**
	 * 定时调度任务
	 */
	private ScheduledFuture handler;

	
	//@Autowired
	//private ActivityService activityService;
	
	/**
	 * tomcat运行时开启线程
	 */
	@Override
	public void init(ServletConfig config) throws ServletException
	{
//		 ServletContext context = config.getServletContext();
//		 WebApplicationContext webApplicationContext =WebApplicationContextUtils.getWebApplicationContext(context);
//		 AutowireCapableBeanFactory autowireCapableBeanFactory = webApplicationContext.getAutowireCapableBeanFactory();
//		 activityService = (ActivityService)autowireCapableBeanFactory.configureBean(this, "activityService");
		try
		{
			ActivityRunnable ar = new ActivityRunnable();
			Calendar cal = Calendar.getInstance();
			int nowHour = cal.get(Calendar.HOUR_OF_DAY);
			int nowMinute = cal.get(Calendar.MINUTE);
			// 计算第一次运行的时间延迟,之后每隔一天运行一次
			int initDelay = (24 - nowHour) * 60 + 60 - nowMinute;
			handler = scheduler.scheduleAtFixedRate(ar, initDelay, 12 * 60, TimeUnit.MINUTES);
			//handler = scheduler.scheduleAtFixedRate(ar, 5, 10, TimeUnit.SECONDS);
		}
		catch (Exception e)
		{
			destroy();
		}
	}

	/**
	 * tomcat退出时结束线程
	 */
	@Override
	public void destroy()
	{
		if (handler != null)
		{
			handler.cancel(true);
		}
		scheduler.shutdown();
	}

	class ActivityRunnable implements Runnable
	{
		private DaoActivityImpl daoActivity;
		@Override
		public void run()
		{
			daoActivity = new DaoActivityImpl();
			try
			{
				daoActivity.setSqlMapClient(DaoConfig.getSqlMapClient());
			}
			catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// 从数据库中读取当前所有正在运行和未运行的活动
			List<ActivityBean> list = daoActivity.queryUnDeadActivities();
			Date nowTime = new Date();
			// 判断当前时间和活动的开始\结束时间,修改活动的状态
			for (ActivityBean bean : list)
			{
				Date beginTime = bean.getBeginTime();
				Date endTime = bean.getEndTime();
				if (endTime.before(nowTime))
				{
					// 活动结束
					daoActivity.modifyActivityStatus(Constant.ACTIVITY_STATUS_END,
							bean.getAid(), bean.getNick());
				}
				else if (beginTime.before(nowTime))
				{
					// 活动开始
					daoActivity.modifyActivityStatus(Constant.ACTIVITY_STATUS_RUNNING,
							bean.getAid(), bean.getNick());
				}
			}
		}

	}
}
